const {
    Router
} = require("express")
const Record = require("../model/Record")
const Hotel = require("../model/Hotel")
const router = new Router()

// 浏览记录的所有数据请求
router.get("/recordList", async (req, res) => {
    const userId = req.userInfo._id
    // const list = await Hotel.find({}).limit(10*num);
    // 查询所有的该用户点赞信息
    const list = await Record.find({ userId }).populate(["userId", "hotelId"]);
    res.status(200).json({ list });
})

// 查询浏览记录是否存在
router.get("/cxrecord", async (req, res) => {
    const userId = req.userInfo._id
    const {
        hotelId,
    } = req.query;
    const list = await Record.findOne({ userId, hotelId });
    if (list) {
        res.status(200).json({ list});
        return
    }
})



// 添加浏览记录
router.post("/addrecord", async (req, res) => {
    // console.log(req.userInfo);
    const userId = req.userInfo._id
    // console.log(userId);
    let formData
    let upnumobj = {};
    const {
        hotelId,
    } = req.body;
    let addhouobj = {
        userId,
    }
    addhouobj.hotelId = hotelId
    upnumobj._id = hotelId
    formData = Hotel;


    const data = await Record.create(addhouobj);
    await formData.findOneAndUpdate(upnumobj, { $inc: { collectNum: 1 } });
    res.status(200).json({
        status: 200,
        message: "添加成功",
        data
    });
})



// 删除浏览记录的数据
// '/api/likey/deletecollect'
router.delete('/deleterecord', async (req, res) => {
    const userId = req.userInfo._id
    let formData;
    let dowmnumobj = {};
    let {
        hotelId,
    } = req.query;

    let delhouobj = {
        userId,
    }
        delhouobj.hotelId = hotelId
        formData = Hotel;
        dowmnumobj._id = hotelId

    // console.log(delhouobj);
    const data= await Record.findOneAndDelete(delhouobj);

    await formData.findOneAndUpdate(dowmnumobj, { $inc: { collectNum: -1 } });
    res.status(200).json({
        status: 200,
        message: "删除成功",
        data
    });
})


module.exports = router